KISSY.add('PicMore',function(S){
    var picmore = {};
    S.mix(picmore,{
        init:function(trigger,hoverCls){
            S.all(trigger).on('mouseover mouseout',function(){
                S.all(this).toggleClass('hover');
            });
        }
    });
    return picmore;
},{
    requires:['sizzle']
});
KISSY.add('Slide',function(S){
    var $ = S.all;
    var slide = {};
    S.mix(slide,{
        current:0,
        url:'',
        init:function(btn_pre,btn_next,url){
            var self = this;
            var pre = $(btn_pre);
            var next = $(btn_next);

            if(pre.length){
                pre.on('click',function(){
                    self.pre();
                    return false;
                })
            }
            if(next.length){
                next.on('click',function(){
                    self.next();
                    return false;
                })
            }
        },
        pre:function(){
            if(this.current>0) this.current--;
            this.getPhoto();
        },
        next:function(){
            this.current++;
            this.getPhoto();
        },
        callback:function(data){
            $('#J_photo').html('<img src="'+data.photo+'" />');
            if(data.info){
                var info = $('#J_info');
                var s = '';
                for(var k in data.info){
                    s += '<li><span class="label">'+k+'</span>'+data.info[k]+'</li>';
                }
                info.html(s);
            }
        },
        getPhoto:function(){
            var self = this;
            S.io({
                url:self.url,
                data:{id:self.current},
                type:'post',
                dataType:'json',
                success:function(data){
                    if(data.stat){
                        self.callback(data);
                    }
                }
            });
        }
    });
    return slide;
});
KISSY.add('Suggest',function(S){
    var $ = S.all;
    var suggest = {};
    S.mix(suggest,{
        url:'',
        list:null,
        trigger:null,
        cache:{},
        init:function(trigger,list,url){
            var self = this;
            this.trigger = $(trigger);
            this.list = $(list).length ? $(list) : null;
            if(!this.list || !trigger.length) return;
            self.trigger.on('keyup',function(){
                if(S.trim(self.trigger.val()).length) self.update();
            });
            this.list.delegate('mouseover mouseout',list+' li',function(e){
                $(e.currentTarget).toggleClass('hover');
            })
            .delegate('click',list+' li',function(e){
                var target = $(e.currentTarget);
                self.trigger.val(target.text());
                self.list.hide();
            });
            $(document).on('click',function(){
                self.list.hide();
            });
        },
        callback:function(data){
            this.list.show();
        },
        update:function(){
            var self = this;
            var val = self.trigger.val();
            if(self.cache[val]){
                self.callback(self.cache[val]);
            }else{
                S.io({
                    url:self.url,
                    dataType:'json',
                    type:'post',
                    success:function(data){
                        if(data.stat){
                            self.cache[val] == data.list;
                            self.callback(self.cache[val]);
                        }
                    }
                })
            }

        }
    });
    return suggest;
})
KISSY.use('PicMore,Slide,Suggest',function(S,PicMore,Slide,Suggest){
    KISSY.ready(function(){
        PicMore.init('.pic');
        Slide.init('#btn_pre','#btn_next','#')
        Suggest.init('#J_search','#J_suggest','#');
    });
});
